<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta
  name="viewport"
  content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1, user-scalable=no"
/>
<meta content="yes" name="apple-mobile-web-app-capable" />
<meta content="black" name="apple-mobile-web-app-status-bar-style" />
<meta content="telephone=no" name="format-detection" />
<script>
  var calArr = [0, 0, 0, 0]

  //全排列
  function allRank(arr) {
    // 临时变量，用于输出
    var temp = []
    var result = []
    function n(arr) {
      for (var i = 0; i < arr.length; i++) {
        // 插入第i个值
        temp.push(arr[i])
        // 复制数组
        var copy = arr.slice()
        // 删除复制数组中的第i个值，用于递归
        copy.splice(i, 1)
        if (copy.length == 0) {
          // 如果复制数组长度为0了，则打印变量
          //console.log(temp);
          result.push([...temp])
        } else {
          // 否则进行递归
          n(copy)
        }
        // 递归完了之后删除最后一个元素，保证下一次插入的时候没有上一次的元素
        temp.pop()
      }
    }
    n(arr)
    return result
  }

  //生成表达式
  function createExprArr(result) {
    var exprArr = []
    //去重
    function distinctAddExpr(expr) {
      if (exprArr.indexOf(expr) == -1) {
        exprArr.push(expr)
      }
    }

    var operation = ['+', '-', '*', '/']
    var count = 0
    for (var i = 0; i < result.length; i++) {
      //24
      for (var o = 0; o < operation.length; o++) {
        //4
        for (var p = 0; p < operation.length; p++) {
          //4
          for (var q = 0; q < operation.length; q++) {
            count = count + 1
            var expr1 =
              result[i][0] +
              operation[o] +
              result[i][1] +
              operation[p] +
              result[i][2] +
              operation[q] +
              result[i][3]
            distinctAddExpr(expr1)

            if (
              isAddAndSubtraction(operation[o]) ==
                isAddAndSubtraction(operation[p]) &&
              isAddAndSubtraction(operation[p]) ==
                isAddAndSubtraction(operation[q])
            ) {
            }
            //第一个为加减
            if (isAddAndSubtraction(operation[o])) {
              var expr2 =
                '(' +
                result[i][0] +
                operation[o] +
                result[i][1] +
                ')' +
                operation[p] +
                result[i][2] +
                operation[q] +
                result[i][3]
              distinctAddExpr(expr2)
              if (isAddAndSubtraction(operation[p])) {
                var expr3 =
                  result[i][0] +
                  operation[o] +
                  '(' +
                  result[i][1] +
                  operation[p] +
                  result[i][2] +
                  ')' +
                  operation[q] +
                  result[i][3]
                distinctAddExpr(expr3)
                var expr4 =
                  '(' +
                  result[i][0] +
                  operation[o] +
                  result[i][1] +
                  operation[p] +
                  result[i][2] +
                  ')' +
                  operation[q] +
                  result[i][3]
                distinctAddExpr(expr4)
              }
            }
            //第二个为加减
            if (isAddAndSubtraction(operation[p])) {
              var expr5 =
                result[i][0] +
                operation[o] +
                '(' +
                result[i][1] +
                operation[p] +
                result[i][2] +
                ')' +
                operation[q] +
                result[i][3]
              distinctAddExpr(expr5)
              if (isAddAndSubtraction(operation[p])) {
                var expr6 =
                  result[i][0] +
                  operation[o] +
                  '(' +
                  result[i][1] +
                  operation[p] +
                  result[i][2] +
                  operation[q] +
                  result[i][3] +
                  ')'
                distinctAddExpr(expr6)
              }
            }
            //第三个为加减
            if (isAddAndSubtraction(operation[q])) {
              var expr7 =
                result[i][0] +
                operation[o] +
                result[i][1] +
                operation[p] +
                '(' +
                result[i][2] +
                operation[q] +
                result[i][3] +
                ')'
              distinctAddExpr(expr7)
            }
          }
        }
      }
    }
    return exprArr
  }

  //是否是加减
  function isAddAndSubtraction(operatorType) {
    if (operatorType == '+' || operatorType == '-') {
      return true
    } else {
      return false
    }
  }

  function create4Number() {
    document.getElementById('result_content').innerHTML = ''
    var content = ''
    var _arr = []
    for (let i = 0; i < 4; i++) {
      var randNum = Math.floor(Math.random() * 10) + 1
      _arr.push(randNum)
      content = content + (i == 0 ? '' : ',') + randNum
    }
    calArr = _arr
    document.getElementById('question_content').innerHTML = content
  }

  function Caculate24() {
    var allrankArr = allRank(calArr)
    var exprArr = createExprArr(allrankArr)
    var methodCount = 0
    var content = ''
    for (var i = 0; i < exprArr.length; i++) {
      if (eval(exprArr[i]) == 24) {
        methodCount = methodCount + 1
        content = content + exprArr[i] + '</br>'
      }
    }
    console.log('计算方法:' + methodCount)
    document.getElementById('result_content').innerHTML = content
  }

  function createResult() {}
</script>

<div style="text-align: center">
  <button style="font-size: 30px; margin-top: 50px" onclick="create4Number()">
    生成4位数字
  </button>
  <div
    style="height: 100px; width: 100vw; line-height: 100px; font-size: 30px"
    id="question_content"
  ></div>
  <button style="font-size: 30px" onclick="Caculate24()">获取答案</button>
  <div
    style="height: 200px; width: 100vw; font-size: 30px; margin-top: 20px"
    id="result_content"
  ></div>
</div>
